<?php

/**
 * Logger
 * Logger::getInstance();
 * Logger::log("Message");
 */
// class Logger extends Singleton
class Logger
{
  use Singleton;
  /**
   * Write a log entry to the opened file resource.
   */
  public function writeLog(string $file_name, string $message, $period = 'd'): void
  {
    $date = '';

    switch ($period) {
      case 'm':
        $date .= local_date('Ym');
        break;

      case 'w':
        $date .= local_date('YW');
        break;

      case 'd':
      default:
        $date .= local_date('Ymd');
        break;
    }

    file_it("[" . local_date('Y-m-d H:i:s') . "] " . $message . ";\n", 'log', $file_name . '_' . $date, true);
  }

  /**
   * log shortcut
   * $period ['d','w','m']
   */
  public static function log(string $file_name, string $message, string $period = 'd'): void
  {
    $logger = static::getInstance();
    $logger->writeLog($file_name, $message, $period);
  }
}
